package baidu.guessnum;
//blog http://hi.baidu.com/wzwahl36
//author wzwahl36
import java.util.ArrayList;

public class Methods
{

	public Methods()
	{
	}

	public static int[] RandomNum()
	{
		int guessNum[] = new int[4];
		ArrayList<Integer> Temp = new ArrayList<Integer>();
		for (int i = 0; i < 4; i++)
			guessNum[i] = -1;

		for (int i = 0; i < 4; i++)
		{
			do
			{
				float tempNum = (float)Math.random() * 10F;
				if ((double)tempNum >= 9.5D)
					guessNum[i] = 0;
				else
					guessNum[i] = Math.round(tempNum);
			} while (Temp.contains(Integer.valueOf(guessNum[i])));
			Temp.add(Integer.valueOf(guessNum[i]));
		}

		return guessNum;
	}

	public static boolean CheckIsFourNum(String enterString)
	{
		boolean IsFourNum = true;
		try
		{
			Integer.valueOf(enterString);
		}
		catch (NumberFormatException e)
		{
			IsFourNum = false;
		}
		if (enterString.length() != 4)
			IsFourNum = false;
		return IsFourNum;
	}

	public static String GetResult(int Num[], String enterString)
	{
		String resultString = "";
		int rightInAll = 0;
		int rightOnlyInNum = 0;
		for (int i = 0; i < 4; i++)
		{
			if (Num[3 - i] == Integer.valueOf(enterString.substring(i, i + 1)).intValue())
				rightInAll++;
			for (int j = 0; j < 4; j++)
				if (Num[i] == Integer.valueOf(enterString.substring(j, j + 1)).intValue())
					rightOnlyInNum++;

		}

		resultString = (new StringBuilder("\t").append(rightInAll)).append("A").append(rightOnlyInNum).append("B").toString();
		return resultString;
	}

	public static boolean IsGuessed(String guessString, String guessedStrings[])
	{
		boolean isGuessed = false;
		for (int i = 0; i < guessedStrings.length; i++)
			if (guessString.equals(guessedStrings[i]))
				isGuessed = true;

		return isGuessed;
	}

	public static boolean HasSameNum(String guessString)
	{
		boolean hasSameNum = false;
		for (int i = 0; i < guessString.length(); i++)
		{
			for (int j = 0; j < guessString.length(); j++)
			{
				if (i == j || !guessString.substring(i, i + 1).equals(guessString.substring(j, j + 1)))
					continue;
				hasSameNum = true;
				break;
			}

		}

		return hasSameNum;
	}
}
